import java.util.HashMap;

public class PrefixSum {
    public int subarraysDivByK(int[] nums, int k) {
        int n = nums.length;
        HashMap<Integer, Integer> hash = new HashMap<>();
        hash.put(0, 1);
        int sum = 0;
        int count = 0;
        for (int i = 0; i < n; i++) {
            sum += nums[i];
            // int sum1 = sum < 0 ? sum * -1 : sum;
            // int ret = (sum % k + k) % k;
            int ret = (sum % k < 0) ? sum % k + k : sum % k;
            count += hash.getOrDefault(ret, 0);
            hash.put(ret, hash.getOrDefault(ret, 0) + 1);
        }
        return count;
    }
}
